package com.lizhangjun.nim.session;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.yeauty.pojo.Session;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * im连接管理
 * @author lizhangjun
 */
public class SessionManager
{
    @Data
    private static class GroupSession{

        private String groupId;

        private String userId;

        private Session session;

        public GroupSession(String groupId, String userId, Session session) {
            this.groupId = groupId;
            this.userId = userId;
            this.session = session;
        }
    }

    /**
     * 匿名连接池
     */
    private static ArrayList<Session> anonymousSession = new ArrayList<>();
    private static Map<String,List<Session>> userSession = new ConcurrentHashMap<>(128);
    private static List<GroupSession> groupSession = new ArrayList<>();


    public static  void add(String groupId, String userId, Session session){
        if (StringUtils.isAllBlank(groupId, userId)){
            // 广播
            anonymousSession.add(session);
        }else if (StringUtils.isBlank(groupId) && StringUtils.isNotBlank(userId)){
            // 单播
            List<Session> sessionList = userSession.get(userId);
            if (sessionList != null && sessionList.size() > 0){
                userSession.get(userId).add(session);
            }else {
                List<Session> sessions = new ArrayList<>();
                sessions.add(session);
                userSession.put(userId,sessions);
            }
        }else {
            // 组播
            groupSession.add(new GroupSession(groupId, userId, session));
        }
    }

    /**
     * 返回匿名连接池
     * @return 匿名连接池
     */
    public static List<Session> get(){
        return anonymousSession;
    }

    /**
     * 获取用户连接
     * @param userId 用户id
     * @return 用户连接集合
     */
    public static List<Session> get(String userId){
        return userSession.get(userId);
    }

    /**
     * 根据用户id集合获取连接集合
     * @param userList 用户id结合
     * @return 连接集合
     */
    public static List<Session> get(List<String> userList){
        List<Session> sessionList = new ArrayList<>();
        for (String userId : userList){
            sessionList.addAll(get(userId));
        }
        return sessionList;
    }

    /**
     * 获取用户组连接
     * @param groupId 组id
     * @param userId 用户id
     * @return 用户组连接集合
     */
    public static List<Session> get(String groupId, String userId){
        List<Session> sessionList = new ArrayList<>();
        for (GroupSession e : groupSession){
            if (e.getGroupId().equals(groupId) && e.getUserId().equals(userId)){
                 sessionList.add(e.getSession());
            }
        }
        return sessionList;
    }

    /**
     * 获取组用户
     * @param groupId 组id
     * @return 连接集合
     */
    public static List<Session> getGroup(String groupId){
        List<Session> sessionList = new ArrayList<>();
        for (GroupSession e : groupSession){
            if (e.getGroupId().equals(groupId)){
                sessionList.add(e.getSession());
            }
        }
        return sessionList;
    }

    /**
     * 获取所有连接
     * @return 所有连接
     */
    public static List<Session> getAll(){
        //获取所有用户连接
        Collection<List<Session>> lists = userSession.values();
        for (List<Session> sessionList : lists){
            anonymousSession.addAll(sessionList);
        }
        return anonymousSession;
    }

    /**
     * 关闭匿名连接
     * @param session 匿名连接
     */
    public static void remove(Session session){
        if (session.isOpen() || session.isActive()){
            anonymousSession.remove(session);
        }
    }

    /**
     * 关闭某一用户的某个连接
     * @param userId 用户id
     * @param session 连接
     */
    public static void remove(String userId, Session session){
        List<Session> sessionList = userSession.get(userId);
        if (sessionList != null && sessionList.size() > 0){
            sessionList.remove(session);
        }
    }

    /**
     * 关闭用户组某个用户连接
     * @param groupId 组id
     * @param userId 用户id
     */
    public static void remove(String groupId, String userId){
        if (groupSession != null && groupSession.size() > 0){
            groupSession.removeIf(e -> e.getGroupId().equals(groupId) && e.getUserId().equals(userId));
        }
    }

    /**
     * 关闭某个用户所有连接
     * @param userId 用户id
     */
    public static void remove(String userId){
        userSession.remove(userId);
    }

    /**
     * 清空连接
     */
    public static void clear(){
        userSession.clear();
        anonymousSession.clear();
        groupSession.clear();
    }

    /**
     * 获取连接数量
     * @return 连接数量
     */
    public static String count(){
        String str = "广播用户数：" + anonymousSession.size();
        str += "，单播用户数：" + userSession.size();
        str += "，组播用户数：" + groupSession.size();
        return str;
    }
}
